package com.ruoyi.redtea.dualprevent.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.redtea.dualprevent.domain.bo.TrainAssessRiskRankBo;
import com.ruoyi.redtea.dualprevent.domain.dto.UnRetifyDTO;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainAssessAnalyseUnit;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainConfigTroubleRecytify;
import com.ruoyi.redtea.dualprevent.domain.vo.TrainAnaUnitAssessVo;
import com.ruoyi.redtea.dualprevent.mapper.TrainAssessAnalyseUnitMapper;
import com.ruoyi.redtea.dualprevent.mapper.TrainConfigTroubleRecytifyMapper;
import com.ruoyi.redtea.dualprevent.service.TrainAssessAnalyseUnitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author lgp
 * @description 针对表【train_assess_analyse_unit(分析单元评估之风险辨识)】的数据库操作Service实现
 * @createDate 2024-05-15 09:32:40
 */
@Service
public class TrainAssessAnalyseUnitServiceImpl implements TrainAssessAnalyseUnitService {

    @Resource
    private TrainAssessAnalyseUnitMapper trainAssessAnalyseUnitMapper;
    @Resource
    private TrainConfigTroubleRecytifyMapper trainConfigTroubleRecytifyMapper;

    @Override
    public List<TrainAnaUnitAssessVo> queryAssessList() {
        PageUtils.startPage();
        List<TrainAnaUnitAssessVo> trainAnaUnitAssessVos = trainAssessAnalyseUnitMapper.queryAssessList();

        for (TrainAnaUnitAssessVo trainAnaUnitAssessVo : trainAnaUnitAssessVos) {
            //读取未处理数量列表
            /**
             * troubleLevel 2  数量：3
             * troubleLevel 1  数量：3
             */
            List<UnRetifyDTO> unRetifyDTOS = trainAssessAnalyseUnitMapper.queryUnRetifyCountByAnalyseId(trainAnaUnitAssessVo.getAnaUnitId());

            List<TrainConfigTroubleRecytify> unRetifyConfigs = trainConfigTroubleRecytifyMapper.selectList(Wrappers.<TrainConfigTroubleRecytify>lambdaQuery()
                    .eq(TrainConfigTroubleRecytify::getConfigCategory, 2));//都是隐患未整改的配置

            HashMap<Integer, TrainConfigTroubleRecytify> levelConigMap = new HashMap<>();
            for (TrainConfigTroubleRecytify config : unRetifyConfigs) {
                levelConigMap.put(config.getConfigLevel(), config);
            }
            int upValue=0;
            for (UnRetifyDTO unRetifyDTO : unRetifyDTOS) {
                TrainConfigTroubleRecytify configTroubleRecytify = levelConigMap.get(unRetifyDTO.getTroubleLevel());
                if(unRetifyDTO.getNumUnRetify()>configTroubleRecytify.getNum()){
                    upValue+=configTroubleRecytify.getUpvalue();
                }
            }
            trainAnaUnitAssessVo.setDynamicRiskLevel(trainAnaUnitAssessVo.getFixRiskLevel()+upValue);
        }
        return trainAnaUnitAssessVos;
    }

    @Override
    public boolean updateIdentifyWay(Integer id, Integer identifyWay) {
        TrainAssessAnalyseUnit trainAssessAnalyseUnit = new TrainAssessAnalyseUnit();
        trainAssessAnalyseUnit.setId(id);
        trainAssessAnalyseUnit.setIdentifyWay(identifyWay);
        return trainAssessAnalyseUnitMapper.updateById(trainAssessAnalyseUnit) == 1;
    }

    //实体类方式  此处暂未调用
    @Override
    public boolean updateAssessRisk(TrainAssessRiskRankBo riskRankBo) {
        return false;
    }

    @Override
    public boolean updateAssessRisk(Integer id, Integer riskLv, Integer assessWay) {
        TrainAssessAnalyseUnit trainAssessAnalyseUnit = new TrainAssessAnalyseUnit();
        trainAssessAnalyseUnit.setId(id);
        trainAssessAnalyseUnit.setFixRiskLevel(riskLv);
        trainAssessAnalyseUnit.setAssessWay(assessWay);
        return trainAssessAnalyseUnitMapper.updateById(trainAssessAnalyseUnit) == 1;
    }

}




